package com.smallkingson.ylib.view.base;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;

import com.smallkingson.ylib.R;
import com.smallkingson.ylib.core.Yr;

public abstract class YView extends View {

	protected TypedArray mTypedArray;

	protected Context mContext;

	public YView(Context context) {

		this(context, null);
	}

	public YView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		mContext = context;
		mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.yr);
	}

	public YView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	protected boolean isCreate;

	@Override
	protected void onDraw(Canvas canvas) {

		if (!isCreate) {
			onCreate();
			isCreate = true;
		}
	}

	protected Bitmap getBitmapByAttrs(int styleable) {

		Bitmap decodeResource = BitmapFactory.decodeResource(
				mContext.getResources(),
				mTypedArray.getResourceId(styleable, 0));

		if (decodeResource == null) {

			try {
				throw new Exception("请加入必要的属性[YSwitch_...]来初始化前景和背景");
			} catch (Exception e) {
				Yr.logError(e);
			}
		}
		return decodeResource;
	}

	protected abstract void onCreate();
}
